package com.ruoyi.web.controller.dm;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.exception.DataRepeatException;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.dm.domain.AirCollectVO;
import com.ruoyi.dm.domain.CollectEstimateVO;
import com.ruoyi.dm.domain.CollectQueryParam;
import com.ruoyi.dm.service.IAirCollectRecordService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

/**
 * 轨道大气探测星座大气密度数据
 */
@Slf4j
@RestController
@RequestMapping("/collect/star")
@Api(tags = "数据采集-轨道大气探测星座大气密度数据")
public class TrackStationCollectController extends BaseController {

    @Autowired
    private IAirCollectRecordService collectService;

    /**
     * 上传
     * @param files
     * @param fileType
     * @return
     */
    @PostMapping ("/upload")
    @ApiOperation("数据上传")
    @ApiImplicitParams(
            {@ApiImplicitParam(name ="satellite",value="卫星",type = "String",required = true,paramType="query",dataTypeClass = String.class),
                    @ApiImplicitParam(name ="dateType",value="数据种类",type = "String",required = false,paramType="query",dataTypeClass = String.class),
            @ApiImplicitParam(name ="fileType",value="文件类型",type = "String",required = true,paramType="query",dataTypeClass = String.class),
                    @ApiImplicitParam(value = "上传文件",name = "files",dataTypeClass = File.class,required = true)
            }
    )
    public R<Boolean> collect(
            @RequestPart("files")MultipartFile[] files,
                      @RequestParam(value = "satellite", required = true)String satellite,
                      @RequestParam(value = "dateType", required = false)String dateType,
                     @RequestParam(value = "fileType", required = true)String fileType) {
        try {
            List<String> res = collectService.uploadFile(files, satellite, fileType, dateType);
            if (res.size()>0) {
                return R.fail(CollectionUtil.join(res,",")+"重复上传,不再处理");
            }
            return R.ok();
        }catch (DataRepeatException e) {
            return R.fail("请勿重复上传");
        }
    }



    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/del")
    @ApiOperation("数据删除(单个)")
    public R<Boolean>  del(@ApiParam(value = "id", name = "id") @RequestParam(value = "id", required = true) Long id) {
        return R.ok(collectService.delFile(id));
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @GetMapping("/delBatch")
    @ApiOperation("数据删除(批量)")
    public R<Boolean>  delBath(@ApiParam(value = "id集合", name = "ids") @RequestParam Long[] ids) {
        return R.ok(collectService.delBatch(ids));
    }

    /**
     * 检索
     * @param param
     * @return
     */
    @ApiOperation("数据查询")
    @PostMapping("/query")
    public R<IPage<AirCollectVO>> query(@ApiParam(name ="param",value="文件检索参数对象",required = false)@Validated @RequestBody CollectQueryParam param) {
        IPage<AirCollectVO> query = collectService.queryStation(param);
        return R.ok(query);
    }

    /**
     * 查看评估结果
     * @param id
     * @return
     */
    @GetMapping("/look")
    @ApiOperation("评估结果")
    public R<CollectEstimateVO> queryRes(@ApiParam(value = "id", name = "id") @RequestParam(value = "id", required = true) Long id) {
        try {
            CollectEstimateVO vo = collectService.lookRes( id);
            return R.ok(vo);
        } catch (Exception e) {
            log.error("",e);
            String s = "文件尚未评估或评估异常";
            return R.fail(s);
        }

    }

    /**
     * 查看评估结果 详情
     * @param id
     * @return
     */
    @GetMapping("/lookDetail")
    @ApiOperation("评估详情")
    public R<String> queryDetailRes(@ApiParam(value = "id", name = "id") @RequestParam(value = "id", required = true) Long id) {
        String s = null;
        try {
            s = collectService.lookDetailRes( id);
            return R.ok(s);
        } catch (Exception e) {
            log.error("",e);
            s = "文件尚未评估或评估异常";
            return R.fail(s);
        }

    }

    /**
     * 下载,返回文件路径
     * @param id
     * @return
     */
    @ApiOperation("数据下载(单个)")
    @GetMapping("/download")
    public void   download(@ApiParam(value = "id", name = "id") @RequestParam(value = "id", required = true) Long id, HttpServletResponse response) {
        String s = collectService.download(id);
        File file = new File(s);
        try {
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, file.getName());
            FileUtils.writeBytes(s, response.getOutputStream());
        }catch (Exception e) {
            log.error("下载失败",e);
        }
    }

    /**
     * 批量下载
     * @param ids
     * @return 文件压缩包路径
     */
    @ApiOperation("数据下载(批量)")
    @GetMapping("/downloadBatch")
    public void  downloadBatch( @ApiParam(value = "文件ids", name = "ids") @RequestParam Long[] ids,HttpServletResponse response) {
        String s = collectService.downloadBatch(ids);
        File file = new File(s);
        try {
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, file.getName());
            FileUtils.writeBytes(s, response.getOutputStream());
        }catch (Exception e) {
            log.error("下载失败",e);
        }
    }

    /**
     * 解析
     * @param id
     * @return
     */
    @ApiOperation("数据解析")
    @GetMapping("/analysis")
    public R<Boolean>  analysis( @ApiParam(value = "id", name = "id") @RequestParam Long id,
                                 @ApiParam(value = "解析方法", name = "method") @RequestParam(required = false) String method) {
        boolean res =  collectService.analysis(id,method);
        return R.ok(res,"开始解析");
    }

    /**
     * 解析
     * @param ids
     * @return
     */
    @ApiOperation("批量解析")
    @GetMapping("/analysisBatch")
    public R<Boolean>  analysisBatch( @ApiParam(name ="ids",value="ids",required = true,example = "1,2") @RequestParam Long[] ids,
                                      @ApiParam(value = "解析方法", name = "method") @RequestParam(required = false) String method) {
        boolean res =  collectService.analysisBatch(ids,method);
        return R.ok(res,"开始解析");
    }
}
